﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Search.aspx.cs" Inherits="DynaBoard.Search" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <style type="text/css">
        td.header { background-color: Gray; color: White; margin: 1px; }
        td { padding: 4px; margin:1px; } 
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Search for: <input type="text" id="searchString" /><input id="search" type="button" value="Search" />
    </div>
    
    <input type="radio" id="titleSearch" name="type" />Post title&nbsp;&nbsp;
    <input type="radio" id="bodySearch" name="type" />Post body
    
    <div id="resultsCount"></div>
    
    <div>
        <table cellpadding="0" cellspacing="0" id="resultsContainer">

        </table>
    </div>
    
    <div id="resultTemplate">
        <!--<tr>
                <td><a href="$URL">$TITLE</a></td>
                <td>$CHILDREN</td>
                <td>$STARTED</td>
                <td>$ACTIVE</td>
                <td>$AUTHOR</td>
            </tr>-->
    </div>
    
    </form>
</body>
</html>

<script type="text/javascript" src="js/jquery-1.3.2-vsdoc2.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript" src="js/Repeater.js"></script>
<script type="text/javascript" src="js/jsonAspNet.js"></script>
<script type="text/javascript">

    function threadHtml(thread) {
        var temp = new Template(document.getElementById("resultTemplate"));
        temp.replace("$URL", thread.Url);
        temp.replace("$TITLE", thread.Title);
        temp.replace("$CHILDREN", thread.Children);
        temp.replace("$ACTIVE", thread.ActiveDateTime);
        temp.replace("$STARTED", thread.StartedDateTime);
        
        var author = thread.Author || "Anonymous";
        
        if (thread.Email) {
            author = "<a href='mailto:"+thread.Email+"'>"+author+"</a>";
        }
        
        temp.replace("$AUTHOR", author);
        
        return temp.markup;
    }

    function gotResults(searchResult) {

        var results = searchResult.Results;

        $("#resultsCount").html("Found " + results.length + " post(s)");

        if (results.length > 0) {

            $("#resultsContainer").html("<tr>" +
            "<td class='header'>Title</td>" +
            "<td class='header'>Replies</td>" +
            "<td class='header'>Started</td>" +
            "<td class='header'>Last Active</td>" +
            "<td class='header'>Author</td></tr>");

            jQuery.each(results, function() {
                $("#resultsContainer").append(threadHtml(this));
            });
        }
        
    }

    $(document).ready(function() {
        $("#search").bind("click", function() {

            //var webMethod = $("#titleSearch").checked() ? "TitleSearch" : "BodySearch";

            var webMethod = "TitleSearch";

            $.jsonAspNet("Search.aspx", webMethod, { searchString: $("#searchString").val(), page: null }, gotResults);
        });
    });

</script>
